#include <iostream>
#include <vector>
#include <algorithm>
#include <sys/time.h>

using namespace std;
//循环的实质是矩阵转置后再以y轴为对称做翻转
void rotate(vector<vector<int>>& matrix) 
{
  int indMax = matrix.size()-1;
  const int loop= (matrix.size()+1)/2;

  for( int j=0; j<loop;++j )
  {
    for( int i=j; i<indMax-j; ++i )  //注意内层循环的界限
    {
      int tmp1 = matrix[j][indMax-i];
      matrix[j][indMax-i] = matrix[i][j];

      int tmp2 = matrix[indMax-i][indMax-j];
      matrix[indMax-i][indMax-j] = tmp1;

      int tmp3 = matrix[indMax-j][i];
      matrix[indMax-j][i] = tmp2;

      matrix[i][j] = tmp3;
    }

  }
}



int main(int argc,char *argv[])
{


  vector< vector<int> > mat;
  mat.push_back( vector<int> {1,2,3,4} );
  mat.push_back( vector<int> {5,6,7,8} );
  mat.push_back( vector<int> {9,10,11,12} );
  mat.push_back( vector<int> {13,14,15,16} );




  struct  timeval    tv1,tv2;
  struct  timezone   tz;
  int time1 = gettimeofday(&tv1,&tz);

  //func();
  rotate(mat);

  int time2 = gettimeofday(&tv2,&tz);
  cout << "time consuming:" << tv2.tv_usec - tv1.tv_usec <<"us"<< endl;

  for(auto iteri=mat.begin();iteri!=mat.end();++iteri)
  {
    for(auto iterj=(*iteri).begin(); iterj!=(*iteri).end(); ++iterj )
    {
      cout<<*iterj<<",";
    }
    cout<<endl;
  }


  return 0;
}
